package com.uplift.mgt.building.service.impl;

import com.github.pagehelper.PageHelper;
import com.uplift.common.Const;
import com.uplift.common.base.ServiceReslut;
import com.uplift.mgt.building.dao.RealtyFileMapper;
import com.uplift.mgt.building.model.RealtyFileModel;
import com.uplift.mgt.building.model.RealtyInfo;
import com.uplift.mgt.building.service.IRealtyFileService;
import com.uplift.mgt.sys.service.IDictService;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.File;
import java.util.*;

@Service
@Transactional
public class RealtyFileServiceImpl implements IRealtyFileService {

	@Autowired
	private RealtyFileMapper realtyFileMapper;

	@Autowired
	private IDictService iDictService;

	@Override
	public List<Map<String, Object>> queryRealtyFile(RealtyFileModel realtyFileModel) {
		PageHelper.startPage(realtyFileModel.getPage(), realtyFileModel.getLimit());
		return addFileHost(this.realtyFileMapper.selectRealtyFile(realtyFileModel));
	}

	@Override
	public List<Map<String, Object>> queryMessageRealtyFile(RealtyFileModel realtyFileModel) {
		PageHelper.startPage(realtyFileModel.getPage(), realtyFileModel.getLimit());
		return addFileHost(this.realtyFileMapper.selectMessageRealtyFile(realtyFileModel));
	}

	@Override
	public ServiceReslut addRealtyFile(RealtyFileModel realtyFileModel) throws Exception {
		if(realtyFileModel.getFiles() != null) {

			String[] fileTypes = StringUtils.split(realtyFileModel.getFileTypes(),",");
			if(fileTypes.length != realtyFileModel.getFiles().length) {
				return ServiceReslut.newServiceReslut(0, "附件与附件类型不匹配。");
			}
			CommonsMultipartFile cmf = null;
			String fileType = null;
			for (int i = 0; i < realtyFileModel.getFiles().length; i++) {
				cmf = realtyFileModel.getFiles()[i];
				fileType = fileTypes[i];

				FileItem item = cmf.getFileItem();
				String fileName = item.getName();
				String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
				String newFileName = UUID.randomUUID().toString().replaceAll("-", "") + "." + fileExt;
				String newFilePath = "product/" + newFileName;
				String rfilePath = "product";
				File dnewFileDir = new File(Const.DIR_UPLOAD_BASE, rfilePath);
				if (!dnewFileDir.exists()) {
					dnewFileDir.mkdirs();
				}
				File file = new File(Const.DIR_UPLOAD_BASE + newFilePath);
				item.write(file);
				// 设置图片路径
				realtyFileModel.setFilePath(newFilePath);
				realtyFileModel.setFileName(fileName);
				realtyFileModel.setBusiType(fileType);
				realtyFileModel.setId(UUID.randomUUID().toString().replaceAll("-", ""));
				realtyFileModel.setCreateTime(new Date());
				realtyFileModel.setState("1");

				realtyFileMapper.insertRealtyFile(realtyFileModel);
			}

			return ServiceReslut.newServiceReslut(1, "");
		}

		return ServiceReslut.newServiceReslut(0, "没有添加任何附件");
	}

	@Override
	public ServiceReslut updateRealtyFile(RealtyFileModel realtyFileModel) throws Exception {
		//删除附件
		deleteFiles(realtyFileModel);
		//添加附件
		return addRealtyFile(realtyFileModel);
	}

	private void deleteFiles(RealtyFileModel realtyFileModel) {
		if(realtyFileModel==null || StringUtils.isBlank(realtyFileModel.getDelFiles())){
			return;
		}

		String[] delFiles = StringUtils.split(realtyFileModel.getDelFiles(),",");
		RealtyFileModel _realtyFileModel = null;
		for (String delFile : delFiles) {
			_realtyFileModel = new RealtyFileModel();
			_realtyFileModel.setId(delFile);
			deleteRealtyFile(_realtyFileModel);
		}
	}

	@Override
	public ServiceReslut deleteRealtyFile(RealtyFileModel realtyFileModel) {
		realtyFileModel.setState("0");		//删除状态
		realtyFileModel.setUpdateTime(new Date());
		return ServiceReslut.newServiceReslut(realtyFileMapper.updateRealtyFile(realtyFileModel), "");
	}

	private List<Map<String, Object>> addFileHost(List<Map<String, Object>> typeFiles) {
		if(typeFiles==null || typeFiles.size()<=0) {
			return null;
		}

//		for (int i = 0; i < typeFiles.size(); i++) {
//			if(typeFiles.get(i).get("filePath") != null) {
//				typeFiles.get(i).put("filePath", fileHostUrl + typeFiles.get(i).get("filePath"));
//			}
//		}

		return typeFiles;
	}
}
